package org.hackday.evetteam;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;

public class AsyncHttp extends Activity {

	private ImageView icon;
	private TextView text;
	
	private AsyncHttp ahp;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajax);
        
        icon = (ImageView) findViewById(R.id.icon);
        text = (TextView) findViewById(R.id.text);
        
       // downloadTwitterIcon();
       // downloadTwitterStream();
        downloadEventsList();
        ahp=this;
    }
    
    
    public void downloadEventsList() {
		Handler handler = new Handler() {
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					Log.d("Twitter Icon", "Starting Connection");
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					String response = (String) message.obj;
					//text.setText(response);
					//Intent i = new Intent(this, EventViewActivity.class);
					Intent i = new Intent(ahp, EventViewActivity.class);
					
					i.putExtra("Response",response);
					startActivity(i);
					break;
				}
				case HttpConnection.DID_ERROR: {
					Exception e = (Exception) message.obj;
					e.printStackTrace();
					break;
				}
				}
			}
		};
		new HttpConnection(handler)
				.get("http://172.16.6.110:8080/EventsServer/nodes.jsp");
	}
    
	public void downloadTwitterIcon() {
		Handler handler = new Handler() {
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					Log.d("Twitter Icon", "Starting Connection");
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					Bitmap response = (Bitmap) message.obj;
					icon.setImageBitmap(response);
					break;
				}
				case HttpConnection.DID_ERROR: {
					Exception e = (Exception) message.obj;
					e.printStackTrace();
					break;
				}
				}
			}
		};
		new HttpConnection(handler)
				.bitmap("http://a1.twimg.com/profile_images/398455304/gvsu-cis-avatar_bigger.jpg");
	}

	public void downloadTwitterStream() {
		Handler handler = new Handler() {
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					text.setText("Starting connection...");
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					String response = (String) message.obj;
					text.setText(response);
					break;
				}
				case HttpConnection.DID_ERROR: {
					Exception e = (Exception) message.obj;
					e.printStackTrace();
					text.setText("Connection failed.");
					break;
				}
				}
			}
		};
		new HttpConnection(handler)
				.get("http://twitter.com/statuses/user_timeline/69177017.rss");
	}
	
}